<script setup>
import { ref } from 'vue';
import { List, Cell, Button, Dialog } from 'vant';
import { getTaskInstancePage, getTaskLog } from '@/api/dolphin';
import { useRoute } from 'vue-router';


const route = useRoute();
const loading = ref(false);
const finished = ref(false);
const currentPage = ref(1);
const pageSize = ref(10);
const totalItems = ref(0);
const items = ref([]);
const value = ref('');
const clusterId = ref(route.params.clusterId || 'default-cluster-id');
const projectCode = ref(Number(route.params.projectCode) || 12345);

const onLoad = async () => {
  try {
    loading.value = true;
    const response = await getTaskInstancePage(
      clusterId.value, // 替换为实际的集群ID
      projectCode.value, // 替换为实际的项目代码
      currentPage.value,
      pageSize.value,
      undefined, // processInstanceId 可选参数
      value.value // 过滤条件
    );
    items.value.push(...response.records);
    totalItems.value = response.totalCount;

    if (items.value.length >= totalItems.value) {
      finished.value = true;
    }
  } catch (error) {
    console.error('加载任务实例失败:', error);
  } finally {
    loading.value = false;
  }
};

const showTaskLog = async (taskInstanceId) => {
  try {
    const logResponse = await getTaskLog(
      clusterId.value, // 替换为实际的集群ID
      projectCode.value, // 替换为实际的项目代码
      taskInstanceId
    );
    Dialog.alert({
      title: '任务日志',
      message: logResponse.logContent, // 根据实际情况调整字段名
    }).then(() => {
      // on close
    });
  } catch (error) {
    console.error('获取任务日志失败:', error);
  }
};
const onSearch = () => {
  currentPage.value = 1;
  items.value = [];
  finished.value = false;
  onLoad();
};
</script>

<template>
  <div>
    <van-search v-model="value" placeholder="任务名称" @search="onSearch"/>
    <van-list v-model:loading="loading" :finished="finished" @load="onLoad">
      <van-cell v-for="(item, index) in items" :key="index" :title="item.name" is-link @click="showTaskLog(item.id)">
        <template #right-icon>
          <van-button round type="success" size="small" icon="passed"></van-button>
          <van-button round type="primary" size="small" icon="description-o"></van-button>
        </template>
      </van-cell>
    </van-list>
  </div>
</template>

<style scoped>
</style>
